import sys
import os
from data_preparation import DataPreparationLayer
from user_information_data_preparation import UserInformationVectorizer


def main():
    """
    汇总脚本：整合数据准备和用户信息推荐功能
    流程：加载数据 → 初始化推荐器 → 生成推荐结果
    """
    print("="*60)
    print("           数据处理与推荐汇总系统           ")
    print("="*60)


    try:
        # 初始化数据准备层
        data_layer = DataPreparationLayer()
        # 添加模板数据
        sample_templates = DataPreparationLayer.load_templates_from_json("/home/ai/Recommendation_system/database/template_data.json")
        print("\n" + "="*50)
        print("添加模板数据".center(40))
        print("="*50)
        data_layer.batch_add_templates(sample_templates)
        
        # 记录用户行为
        print("\n" + "="*50)
        print("记录用户行为".center(40))
        print("="*50)
        DataPreparationLayer.load_and_record_behavior(data_layer, "/home/ai/Recommendation_system/database/user_behavior_samples.json")


        # 保存数据
        print("\n" + "="*50)
        print("保存数据".center(40))
        print("="*50)
        data_layer.save_behavior_data()
        data_layer.export_template_vectors()
        
        # 打印状态报告
        print("\n" + "="*50)
        print("系统状态报告".center(40))
        print("="*50)
        print(f"• 模板数量：{data_layer.get_template_count()}")
        print(f"• 用户数量：{data_layer.get_user_count()}")
        print(f"• 模板1信息：{data_layer.get_template_info(1)}")
        
        # 保存所有用户相似度信息
        print("\n" + "="*50)
        print("保存所有用户相似度信息".center(40))
        print("="*50)
        user_similarities = data_layer._calculate_user_similarity(force_recalculate=True, save_path="/home/ai/Recommendation_system/user_similarities_data.json")
        print(f"• 用户相似度计算结果已保存到JSON文件，共{len(user_similarities)}个用户")


        print("="*50)
        print("用户信息向量转换工具（维度匹配模板）".center(40))
        print("="*50)
        
        vectorizer = UserInformationVectorizer()
        
        print("\n1. 加载原始用户信息...")
        if not vectorizer.load_user_information():
            exit(1)
        
        print("\n2. 生成并存储匹配维度的向量...")
        if not vectorizer.process_and_store_vectors():
            exit(1)
        
        print("\n" + "="*50)
        print("所有操作完成".center(40))
        print("="*50)

    except Exception as e:
        print(f"错误：{str(e)}")
        sys.exit(1)

if __name__ == "__main__":
    main()
